from flask import Flask, render_template, request
from flask_socketio import SocketIO, send


app = Flask(__name__)
app.secret_key = 'jzw'
socketio = SocketIO(app, cors_allowed_origins="*")
m_namespace = '/ws'

@app.route('/')
def index():
    return render_template('test.html')

@socketio.on('message')
def handle_message(message):
    print('received message: %s' % message)

@socketio.on('json')
def handle_json(json):
    print('received json: %s' % str(json))

@socketio.on('my event')
def handle_my_event(json):
    print('received my event: %s' % str(json))
    send('Hello my client!')

@socketio.on('connect')
def handle_connect():
    print('connect with %s' % request.sid)

@socketio.on('connect', namespace=m_namespace)
def handle_connect_in_namespace():
    print('connect with %s in namespace %s' % (request.sid, m_namespace))


if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=5100)
